package cn.zy.gvs.v1.core.config;

import cn.dev33.satoken.interceptor.SaInterceptor;
import cn.dev33.satoken.router.SaRouter;
import cn.dev33.satoken.stp.StpUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Slf4j
@Configuration
public class SaTokenConfigure implements WebMvcConfigurer {

    @Value("${gvs.apiPrefix}")
    private String apiPrefix;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 注册 Sa-Token 拦截器，定义详细认证规则
        log.debug("^_^ 开始初始化Go-View专用权限认证方式...");
        registry.addInterceptor(new SaInterceptor(handler -> {
            SaRouter.match(apiPrefix + "/**").check(r -> StpUtil.checkLogin());
        })).addPathPatterns("/**")
                .excludePathPatterns(
                        apiPrefix + "/sys/login",
                        apiPrefix + "/sys/logout",
                        apiPrefix + "/sys/getOssInfo",
                        apiPrefix + "/project/getData"
                )
        ;
    }
}
